package com.ns.sbs.oauth.menu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.current.config.SysConst;
import com.ns.core.utils.sg.ResultOk;
import com.ns.sbs.oauth.menu.bo.MenuBO;
import com.ns.sbs.oauth.menu.bo.MenuPageBO;
import com.ns.sbs.oauth.menu.dto.MenuPageDto;
import com.ns.sbs.oauth.menu.entry.MenuEntry;
import com.ns.sbs.oauth.menu.service.IMenuService;
import com.ns.sbs.oauth.menu.vo.MenuEntryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 服务菜单对外接口
 *
 * @author lw
 * @since 2024/09/13
 */
@RestController
@RequestMapping("/menu/")
@RequiredArgsConstructor
@CrossOrigin
public class MenuController {

    private final IMenuService menuService;

    /**
     * 增
     *
     * @param bo 参数
     */
    @PostMapping("add")
    @ReqLog(module = "菜单模块", name = "增", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.BASIC_MENU_TREE, key = "1")
    @Transactional(rollbackFor = Exception.class)
    public ResultOk add(@RequestBody MenuBO bo) {
        if (!CollectionUtils.isEmpty(bo.getSelectMenus())) {
            for (List<String> selectMenu : bo.getSelectMenus()) {
                if (!CollectionUtils.isEmpty(selectMenu)) {
                    bo.setPid(Long.parseLong(selectMenu.get(0)));
                }
            }
        }
        MenuEntry menuEntry = new MenuEntry();
        BeanUtils.copyProperties(bo, menuEntry);
        menuEntry.setId(bo.getSort());
        return ResultOk.getByBoolean(menuService.saveOrUpdate(menuEntry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "菜单模块", name = "逻辑删", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.BASIC_MENU_TREE, key = "1")
    public ResultOk delete(Long id) {
        return ResultOk.getByBoolean(menuService.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "菜单模块", name = "批量删", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.BASIC_MENU_TREE, key = "1")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(menuService.removeBatchByIds(bo.getIds()));
    }

    /**
     * 改
     *
     * @param bo 参数
     */
    @PostMapping("update")
    @ReqLog(module = "菜单模块", name = "改", isRecordFlag = true)
    @CacheEvict(cacheNames = SysConst.BASIC_MENU_TREE, key = "1")
    public ResultOk update(@RequestBody MenuBO bo) {
        if (!CollectionUtils.isEmpty(bo.getSelectMenus())) {
            for (List<String> selectMenu : bo.getSelectMenus()) {
                if (!CollectionUtils.isEmpty(selectMenu)) {
                    bo.setPid(Long.parseLong(selectMenu.get(0)));
                    break;
                }
            }
        }
        MenuEntry menuEntry = new MenuEntry();
        BeanUtils.copyProperties(bo, menuEntry);
        return ResultOk.getByBoolean(menuService.saveOrUpdate(menuEntry));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "菜单模块", name = "查")
    public ResultOk getById(Long id) {
        MenuEntry entry = menuService.getById(id);
        MenuEntryVO vo = new MenuEntryVO();
        BeanUtils.copyProperties(entry, vo);
        if (Objects.nonNull(vo.getPid())) {
            // 组装数据选项菜单
            List<List<String>> selectMenus = new ArrayList<>();
            List<String> ms = new ArrayList<>();
            ms.add(String.valueOf(vo.getPid()));
            selectMenus.add(ms);
            vo.setSelectMenus(selectMenus);
        }
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查 - 集合
     */
    @PostMapping("getList")
    @ReqLog(module = "菜单模块", name = "集合")
    public ResultOk getList(@RequestBody MenuPageBO bo) {
        MenuPageDto dto = new MenuPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<MenuEntryVO> page = menuService.selectBy(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }
}
